package com.freshfresh.activity.selfcenter;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.activity.home.HomeActivity;
import com.freshfresh.utils.FreshConstants;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.freshfresh.view.AlertDialog;
import com.hangyjx.widget.dialog.CustomDialog;

public class MoreInfoActivity extends BaseActivity implements OnClickListener{

    private LinearLayout ll_about_fresh;
    private LinearLayout ll_help_center;
    private LinearLayout ll_message_push;
    private Button bt_logout;
    private PopupWindow popupWindow;
    private TextView tv_version_info;
    private CheckBox cb_msg_push;
    private SharedPreferences sp;
	private String userid;
	private String token;
	private ImageView iv_back;
	private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_more);
//        setMyActivity(this);
//        Cache.add(this);
        SharedPreferences sp = FreshConstants
				.getUserShared(MoreInfoActivity.this);
		userid = sp.getString("userid", "");
		token = sp.getString("token", "");
        initView();
        initClick();

    }



    /**
     * 
     * @Description 方法的一句话描述
     * @param 说明参数含义
     * @return 说明返回值含义
     */
    private void initClick() {
        // TODO Auto-generated method stub
    	iv_back.setOnClickListener(this);
        ll_about_fresh.setOnClickListener(this);
        ll_help_center.setOnClickListener(this);
        bt_logout.setOnClickListener(this);
    }

    /**
     * 
     * @Description 获取控件
     * @param 说明参数含义
     * @return 说明返回值含义
     */
    private void initView() {
    	iv_back = (ImageView) findViewById(R.id.iv_back);
    	tv_title = (TextView) findViewById(R.id.tv_title);
    	tv_title.setText("更多");
        ll_about_fresh = (LinearLayout) findViewById(R.id.ll_about_fresh);
        ll_help_center = (LinearLayout) findViewById(R.id.ll_help_center);
        bt_logout = (Button) findViewById(R.id.bt_logout);
    }
    
    //退出
	public  void operateDialog(Context context,final String phone,String title,String message,
			String positiveButtonText,String negativeButtonText){
		CustomDialog.Builder builder = new CustomDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		//确定按钮
		builder.setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				FreshConstants.getUserShared(MoreInfoActivity.this).edit().clear().commit();
				showToast(MoreInfoActivity.this, "退出成功");
				dialog.dismiss();
				Intent intent=new Intent();
				intent.setAction(FreshConstants.LOGOUT);
				sendBroadcast(intent);
			}
		});
		//取消按钮
		builder.setNegativeButton(negativeButtonText, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.createB2().show();
	}

    /**
     * @Description 退出登录提示
     * @param 说明参数含义
     * @return 说明返回值含义
     */
    public void logoutDialog() {
        LayoutInflater li = LayoutInflater.from(MoreInfoActivity.this);
        View view1 = li.inflate(R.layout.popu_view1, null);
        popupWindow = new PopupWindow(view1, WindowManager.LayoutParams.FILL_PARENT,
                WindowManager.LayoutParams.FILL_PARENT, true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        // 弹出框
        popupWindow.showAtLocation(MoreInfoActivity.this.findViewById(R.id.bt_logout),
                Gravity.CENTER, 0, 0);// 设置在屏幕中的显示位置
        TextView tv_popu_content = (TextView) view1.findViewById(R.id.tv_popu_content);
        tv_popu_content.setText("确定要退出登录吗?");

        TextView tv_ok_popu = (TextView) view1.findViewById(R.id.tv_ok_popu);
        TextView tv_cancel_popu = (TextView) view1.findViewById(R.id.tv_cancel_popu);
        tv_ok_popu.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
                if (userid != null && !TextUtils.isEmpty(token)) {
                    logoutRequest();
                }
            }
        });

        tv_cancel_popu.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
    }

    /**
     * 
     * @Description 退出登录请求
     * @param 说明参数含义
     * @return 说明返回值含义
     */
    protected void logoutRequest() {
    	Map<String, String> params = new HashMap<String, String>();
        params.put("customerid", userid);
        params.put("token", token);

        executeRequest(new StringRequest(UrlConstants.customerLogout, params, new Response.Listener<String>(){

			@Override
			public void onResponse(String arg0) {
				if (Utils.parseJsonStr(arg0).get("result").toString().equals("1")) {
					new AlertDialog(MoreInfoActivity.this).builder().setMsg("已成功退出")
                  .setCancelable(false)
                  .setPositiveButton("确定", new OnClickListener()
                  {

                      @Override
                      public void onClick(View arg0) {
                          // TODO Auto-generated method
                          // stub
                          Intent intent = new Intent(MoreInfoActivity.this,
                                  HomeActivity.class);
//                          intent.putExtra(Constants.TAB_NAME,
//                                  MainActivity.class.getSimpleName());
                          startActivity(intent);
                          MoreInfoActivity.this.finish();
                      }
                  }).show();
//
				}
			}
        	
        },new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError arg0) {
				showToast(MoreInfoActivity.this,"退出登录失败， 请稍候再试");
			}
		}));
    }

    /**
     * 
     * @Description 关于两鲜请求
     * @param 说明参数含义
     * @return 说明返回值含义
     */
    private void aboutFreshRequest() {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }



	@Override
	public void onClick(View arg0) {
		 Intent it = null;
	        switch (arg0.getId()) {
	        case R.id.iv_back:
	            MoreInfoActivity.this.finish();
	            break;
	        case R.id.ll_about_fresh:
	        	it = new Intent(MoreInfoActivity.this,SetInfoActivity.class);
	        	it.putExtra("url", "http://www.freshfresh.com/our-story");
	        	it.putExtra("title", "关于两鲜");
	        	startActivity(it);
	            break;
	        case R.id.ll_help_center:
	        	it = new Intent(MoreInfoActivity.this,SetInfoActivity.class);
	        	it.putExtra("url", "http://www.freshfresh.com/guide#answer1");
	        	it.putExtra("title", "帮助中心");
	        	startActivity(it);
	            break;
	        case R.id.bt_logout:
	        	if(!FreshConstants.hasLogined(MoreInfoActivity.this)){
	        		showToast(MoreInfoActivity.this, "你尚未登录");
				}else{
	        	operateDialog(MoreInfoActivity.this, "", "提示", "确定退出登陆吗？", "确定", "取消");
				}
	            break;

	        }
		
	}
}
